package com.hisicom.scqxspxcjc.services;

import cn.hutool.core.util.ObjectUtil;
import com.hisicom.scqxspxcjc.sceneCheck.report.domain.Report;
import com.hisicom.scqxspxcjc.sceneCheck.report.domain.ReportInsp;
import com.hisicom.scqxspxcjc.sceneCheck.report.exception.ReportException;
import com.hisicom.scqxspxcjc.sceneCheck.report.service.ReportInspService;
import com.hisicom.scqxspxcjc.sceneCheck.report.service.ReportService;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.domain.SystemCheck;
import com.hisicom.scqxspxcjc.sceneCheck.systemCheck.service.SystemCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhonggls
 * @date 2020/12/21 14:12
 * @Explain
 */
@Service
@Transactional
public class ReportServiceBus {
    @Autowired
    private ReportService reportService;
    @Autowired
    private ReportInspService reportInspService;
    @Autowired
    private SystemCheckService systemCheckService;

    /**
     * 通过组id获取"现场检查"栏对象集合
     *
     * @param groupId 分组id
     * @return "现场检查"栏对象集合
     */
    public Object findByGroupId(String groupId) {
        List<Report> reportList = reportService.findByGroupId(groupId);
        if (ObjectUtil.isNotEmpty(reportList)) {
            if (reportList.size() > 1) {
                throw new ReportException("分组id出现了重复，请联系管理员");
            }
            Report report = reportList.get(0);
            List<ReportInsp> reportInspList = reportInspService.findByZjzxhId(report.getId());
            report.setJcryList(reportInspList);
            return report;
        } else { //如果未查询到，就初始化
            List<SystemCheck> systemCheckList = systemCheckService.findByGroupId(groupId);
            if (ObjectUtil.isNotEmpty(systemCheckList)) {
                List<String> cpmcList = new ArrayList<>();
                List<String> flbmList = new ArrayList<>();
                List<String> cpslhList = new ArrayList<>();
                for (SystemCheck systemCheck1 : systemCheckList) {
                    cpmcList.add(systemCheck1.getCpmc());
                    flbmList.add(systemCheck1.getFlbm());
                    cpslhList.add(systemCheck1.getAcceptanceNumber());
                }
                Report report = new Report();
                report.setCpmc(cpmcList.toString().replace("[", "").replace("]", "")); //产品名称
                report.setFlbm(flbmList.toString().replace("[", "").replace("]", "")); //分类编码
                report.setCpslh(cpslhList.toString().replace("[", "").replace("]", "")); //产品受理号
                SystemCheck systemCheck = systemCheckList.get(0);
                report.setGroupId(groupId);
                report.setZcsqr(systemCheck.getSqrmc());//注册申请人
                report.setScdz(systemCheck.getScdz());//生产地址
                report.setStf(systemCheck.getWtscqy());//受托方
                report.setFgdggxh(systemCheck.getXhgg()); //覆盖的规格型号
                report.setHclx(systemCheck.getDataType()); //核查类型
                report.setCplb("1");//产品类别
                report.setHcyj(systemCheck.getHcyj());//核查依据
                report.setJcgc("[]");//初始化检查过程
//              report.setHcnr(systemCheck.get());//核查内容
                return report;
            }

        }
        return null;
    }

    /**
     * 保存现场检查报告
     *
     * @param report "现场检查"对象集合数据
     * @return 成功/失败
     */
    public void add(Report report) {
        Report reportBack = reportService.saveInfo(report);
        if (ObjectUtil.isNotEmpty(report)) {
            List<ReportInsp> reportInspList = report.getJcryList();
            if (ObjectUtil.isNotEmpty(reportInspList)) {
                for (ReportInsp reportInsp : reportInspList) {
                    reportInsp.setZjzxhId(reportBack.getId());
                }
                reportInspService.deleteByZjzxhId(reportBack.getId());
                reportInspService.saveAll(reportInspList);
            }
        }

    }

}
